package com.netposa.datawarehouse.aggregator.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Splitter;
import com.netposa.datawarehouse.aggregator.service.RegisterService;
import com.netposa.datawarehouse.aggregator.web.model.ModuleTable;
import com.netposa.library.datawarehouse.aggregate.model.ModuleInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class RegisterServiceImpl implements RegisterService {
   private static final Logger log = LoggerFactory.getLogger(RegisterServiceImpl.class);
   @Autowired
   private ModuleTable moduleTable;
   @Value("${netposa.register.ip:#{'127.0.0.1'}}")
   private String registerIp;

   public Object registerModule(ModuleInfo moduleInfo) {
      if (moduleInfo != null && !StringUtils.isBlank(moduleInfo.getName()) && !CollectionUtils.isEmpty(moduleInfo.getInterfaces())) {
         if (!this.ipCheck(moduleInfo.getAddress())) {
            log.warn("register ip is illegal : " + moduleInfo.toString());
            return null;
         } else {
            long start = System.currentTimeMillis();
            log.info("start register module : " + moduleInfo.toString());
            moduleInfo.getResourceTypes().forEach((type) -> {
               this.moduleTable.stringMap(type.getValue(), moduleInfo.getName(), JSON.toJSONString(moduleInfo.getInterfaces()));
            });
            this.moduleTable.putInterfaceInfo(moduleInfo.getName(), moduleInfo.getInterfaces());
            long end = System.currentTimeMillis();
            log.info("finish register module : {}ms", end - start);
            return this.moduleTable.getModuleMap();
         }
      } else {
         log.warn("register module is null : " + moduleInfo.toString());
         return null;
      }
   }

   public Object modules(ModuleInfo moduleInfo) {
      if (moduleInfo != null && !StringUtils.isBlank(moduleInfo.getName())) {
         Map map = new HashMap(1);
         map.put(moduleInfo.getName(), this.moduleTable.getInterfaceInfo(moduleInfo.getName()));
         return map;
      } else {
         return this.moduleTable.getModuleMap();
      }
   }

   public Object delete(ModuleInfo moduleInfo) {
      return null;
   }

   private boolean ipCheck(String address) {
      if (!StringUtils.isBlank(this.registerIp) && !StringUtils.isBlank(address)) {
         List ips = Splitter.on(",").trimResults().splitToList(this.registerIp);
         if (ips.contains("*.*.*.*")) {
            return true;
         } else {
            List ads = Splitter.on(":").trimResults().splitToList(address);
            if (CollectionUtils.isEmpty(ads)) {
               return false;
            } else {
               return ips.contains(ads.get(0));
            }
         }
      } else {
         return false;
      }
   }
}
